package com.hbu.coupon.hbumallcoupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbu.common.TO.SkuReductionTO;
import com.hbu.common.utils.PageUtils;
import com.hbu.common.utils.Query;
import com.hbu.coupon.hbumallcoupon.dao.SkuFullReductionDao;
import com.hbu.coupon.hbumallcoupon.dao.SkuLadderDao;
import com.hbu.coupon.hbumallcoupon.entity.MemberPriceEntity;
import com.hbu.coupon.hbumallcoupon.entity.SkuFullReductionEntity;
import com.hbu.coupon.hbumallcoupon.entity.SkuLadderEntity;
import com.hbu.coupon.hbumallcoupon.service.MemberPriceService;
import com.hbu.coupon.hbumallcoupon.service.SkuFullReductionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author 90458
 */
@Service("skuFullReductionService")
@AllArgsConstructor
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    private SkuLadderDao skuLadderDao;

    private SkuFullReductionDao skuFullReductionDao;

    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSkuReduction(SkuReductionTO skuReduction) {

        Long skuId = skuReduction.getSkuId();
        Integer addOther = skuReduction.getCountStatus();


        if (skuReduction.getFullCount() > 0 && skuReduction.getDiscount().compareTo(BigDecimal.ZERO) > 0) {
            // 1、存储sms_sku_ladder
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuId);
            skuLadderEntity.setFullCount(skuReduction.getFullCount());
            skuLadderEntity.setDiscount(skuReduction.getDiscount());
            skuLadderEntity.setAddOther(addOther);
            skuLadderDao.insert(skuLadderEntity);
        }

        if (skuReduction.getFullPrice().compareTo(BigDecimal.ZERO) > 0 && skuReduction.getReducePrice().compareTo(BigDecimal.ZERO) > 0) {
            // 2、存储sms_sku_full_reduction
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            skuFullReductionEntity.setSkuId(skuId);
            skuFullReductionEntity.setFullPrice(skuReduction.getFullPrice());
            skuFullReductionEntity.setReducePrice(skuReduction.getReducePrice());
            skuFullReductionEntity.setAddOther(addOther);
            skuFullReductionDao.insert(skuFullReductionEntity);
        }


        // 3、存储sms_member_price
        memberPriceService.saveBatch(skuReduction.getMemberPrice().stream()
                .filter(memberPrice -> memberPrice.getPrice().compareTo(BigDecimal.ZERO) > 0)
                .map(memberPrice -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuId);
                    memberPriceEntity.setMemberLevelId(memberPrice.getId());
                    memberPriceEntity.setMemberLevelName(memberPrice.getName());
                    memberPriceEntity.setMemberPrice(memberPrice.getPrice());
                    memberPriceEntity.setAddOther(addOther);
                    return memberPriceEntity;
                }).collect(Collectors.toList()));
    }

}